package com.leyou.order.interceptor;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.threadlocals.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/20 0020
 * Time: 9:37
 */

public class UserInterceptor implements HandlerInterceptor {
    private static final String HEADER_NAME = "user_info";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求头
        String header = request.getHeader(HEADER_NAME);
        if (StringUtils.isBlank(header)) {
            throw new LyException(ExceptionEnum.UNAUTHORIZED);
        }
        //保存用户信息
        UserHolder.setUser(Long.valueOf(header));
        return true;
    }
}
